package org.kzwl.book.controller.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.AdminQueryNewsBO;
import org.kzwl.comm.pojo.bo.NewsBO;
import org.kzwl.comm.pojo.bo.news.NewsBasicBO;
import org.kzwl.comm.pojo.bo.news.QueryDetailsBO;
import org.kzwl.comm.pojo.dto.news.NewsDTO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.web.News;
import org.kzwl.service.web.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/NewsController")
@Api("PC新闻业务")
public class NewsController {

    @Autowired
    NewsService newsService;

    @ApiOperation(value = "发布新闻")
    @PostMapping("/saveOne")
    @Valid
    public ResponseData<Void> saveOne(HttpSession session, @Valid @RequestBody NewsBO newsBO) {
        if (newsBO.getCuNewsContent().equals("")) {
            throw new BizException("内容不能为空");
        }
        if (newsBO.getCuNewsTitle().equals("")) {
            throw new BizException("标题不能为空");
        }
        if (newsBO.getCuNewsSource().equals("")) {
            throw new BizException("来源不能为空不能为空");
        }
        if (newsBO.getCuUserId() == null) {
            throw new BizException("发布人为空不能为空");
        }
        return newsService.saveOne(newsBO);
    }

    @ApiOperation(value = "后台--查询新闻")
    @PostMapping("/adminQuery")
    public ResponseData<IPage<News>> adminQuery(@RequestBody @Valid AdminQueryNewsBO newsBO) {
        return newsService.adminQuery(newsBO);
    }

    @ApiOperation(value = "后台--查询一条新闻")
    @PostMapping("/queryOne")
    public ResponseData<News> queryOne(@RequestBody Long Id) {
        return newsService.queryOne(Id);
    }


    @ApiOperation(value = "后台--修改一条新闻")
    @PostMapping("/updateOne")
    public ResponseData<Void> updateOne(@RequestBody NewsBO newsBO) {
        return newsService.updateOne(newsBO);
    }

    @ApiOperation(value = "后台--删除一条新闻")
    @PostMapping("/deleteId")
    public ResponseData<Void> deleteId(@RequestBody Long Id) {
        return newsService.deleteId(Id);
    }


    @ApiOperation(value = "首页新闻查询", notes = "cuTownId(镇馆id)--必填")
    @PostMapping("/indexQuery")
    public ResponseData<List<NewsDTO>> indexQuery(@RequestBody NewsBasicBO newsBO) {
        List<NewsDTO> list = newsService.indexQuery(newsBO);
        return new ResponseData<>(list);
    }

    @ApiOperation(value = "查询新闻", notes = "cuTownId(镇馆id)--必填 cuCategoryId(分类  0-全部 1.本县资讯 2 .文件通告 3.新闻资讯)--必填 current(页码)--必填 size(条数)--选填")
    @PostMapping("/queryBasic")
    public ResponseData<IPage<NewsDTO>> queryBasic(@RequestBody NewsBasicBO newsBO) {
        IPage<NewsDTO> page = newsService.queryBasic(newsBO);
        page.getRecords().forEach(l -> {
            String s = MyUtils.regExHtml(l.getCuNewsContent());
            l.setCuNewsContent(s);
            l.setCuNewsContent(l.getCuNewsContent().substring(0, Math.min(l.getCuNewsContent().length(), 30)));
        });
        return new ResponseData<>(page);
    }

    @ApiOperation(value = "查询新闻详细", notes = "cuNewsId(新闻编号)--必填")
    @PostMapping("/queryDetails")
    public ResponseData<NewsDTO> queryDetails(@RequestBody QueryDetailsBO newsBO) {
        NewsDTO dto = newsService.queryDetails(newsBO);
        return new ResponseData<>(dto);
    }
}
